﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools.UserControls;

namespace Tools
{
    public partial class Tasks : Form
    {
        public List<TaskModel> TaskModels { get; set; }

        public Tasks()
        {
            InitializeComponent();
            SetDefaultLocation();
            GetTasks();
        }

        #region 获取任务

        /// <summary>
        /// 获取任务
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        private async void GetTasks()
        {
            await Helper.MicrosoftToken.SetTokenAsync();

            var filter = $"status ne 'completed'";
            var orderby = $"createdDateTime desc";
            var url = $"https://graph.microsoft.com/beta/me/outlook/tasks?$filter={filter}&$orderby={orderby}&$top=999";
            var res = Helper.MicrosoftToken.HttpRequest(url, "", "get");
            string value = JsonConvert.DeserializeObject<dynamic>(res).value.ToString();
            this.TaskModels = JsonConvert.DeserializeObject<List<TaskModel>>(value);


            DataBindData(1, "重要又紧急", "AQMkADAwATY3ZmYAZS1hOGQ5LWI1NjEtMDACLTAwCgAuAAAD63Q7MReF3U_07sh48tkOlAEA2ME1sP4OHkG8uWM1VkagLgADcRQylgAAAA==");
            DataBindData(2, "重要不紧急", "AQMkADAwATY3ZmYAZS1hOGQ5LWI1NjEtMDACLTAwCgAuAAAD63Q7MReF3U_07sh48tkOlAEA2ME1sP4OHkG8uWM1VkagLgADcuAO6QAAAA==");
            DataBindData(3, "不重要但紧急", "AQMkADAwATY3ZmYAZS1hOGQ5LWI1NjEtMDACLTAwCgAuAAAD63Q7MReF3U_07sh48tkOlAEA2ME1sP4OHkG8uWM1VkagLgADcuAO6gAAAA==");
            DataBindData(4, "不重要不紧急", "AQMkADAwATY3ZmYAZS1hOGQ5LWI1NjEtMDACLTAwCgAuAAAD63Q7MReF3U_07sh48tkOlAEA2ME1sP4OHkG8uWM1VkagLgADcuAO6wAAAA==");
        }
        #endregion

        #region 绑定任务

        private void DataBindData(int i, string title, string folderID)
        {
            var name = $"userTask{i}";
            foreach (UserTask item in this.tableLayoutPanel1.Controls)
            {
                if (item.Name == name)
                {
                    item.TaskModels = this.TaskModels.Where(ex => ex.parentFolderId == folderID).ToList();
                    item.ControlName = name;
                    item.Title = title;
                    item.FolderID = folderID;
                    item.AddEvent += Item_AddEvent;
                    item.InitBind();
                }
            }
        }

        private void Item_AddEvent(string ControlName, string title, string folderID)
        {
            this.label_ControlName.Text = ControlName;
            this.label_type.Text = title;
            this.label_folderid.Text = folderID;
        }
        #endregion

        #region 设置默认位置

        /// <summary>
        /// 默认位置
        /// </summary>
        public void SetDefaultLocation()
        {
            Rectangle rect = new Rectangle();
            rect = Screen.GetWorkingArea(this);

            this.Left = (Convert.ToInt32(rect.Width * 0.8) - this.Width) / 2;
            this.Top = (Convert.ToInt32(rect.Height * 0.8) - this.Height) / 2;
        }

        #endregion

        #region 添加

        private void button1_Click(object sender, EventArgs e)//添加
        {
            var url = $"https://graph.microsoft.com/beta/me/outlook/taskFolders/{this.label_folderid.Text}/tasks";
            var obj = new
            {
                subject = txt_title.Text,
                body = new
                {
                    contentType = "Text",
                    content = txt_nr.Text
                }
            };
            Helper.MicrosoftToken.HttpRequest(url, JsonConvert.SerializeObject(obj));

            //刷新对应栏目
            foreach (UserTask item in this.tableLayoutPanel1.Controls)
            {
                if (item.Name == this.label_ControlName.Text)
                {
                    GetTasks();
                    item.InitBind();
                }
            }
            txt_title.Text = "";
            txt_nr.Text = "";
        }
        #endregion


    }
}
